package apps.AfterDlcs.afterDlcsBL.world;

import apps.AfterDlcs.AfterDlcs;
import apps.AfterDlcs.afterDlcsBL.AfterDlcsThread;
import apps.AfterDlcs.afterDlcsBL.Event;
import apps.AfterDlcs.afterDlcsBL.buff.BuffKeys;
import apps.AfterDlcs.afterDlcsBL.hero.Hero;
import apps.AfterDlcs.afterDlcsBL.warSand.WarSand;
import backs.dataBase.DBNormal;

import java.util.HashMap;
import java.util.Map;

public class World
{
    int worldID;

    public static Map<Integer,World> worldMap =new HashMap<>();

    public static World getWorld(int worldID)
    {
        if(!worldMap.containsKey(worldID))
        {
            worldMap.put(worldID,new World(worldID));
        }
        return worldMap.get(worldID);
    }

    public World(int worldID)
    {
        this.worldID=worldID;
        initWorld();
    }

    public void initWorld()
    {

        for(int i=WorldBL.START_HERO_ID;i<=WorldBL.getNowHeroID(worldID);i++)
        {
            Hero.getHero(worldID,i);
        }
    }

    public void timePass()
    {
        for(String key:WarSand.warSandMap.keySet())
        {
            WarSand warSand=WarSand.warSandMap.get(key);
            warSand.handleEvent(new Event(null,null,null, BuffKeys.TIME_PASS,new Object[]{}));
        }
    }
    public void resetWorld()
    {
        AfterDlcsThread.stop=true;
        DBNormal.delValue(AfterDlcs.dbName,worldID);
        WarSand.warSandMap.clear();
        Hero.heroMap.clear();
        for(int key:worldMap.keySet())
        {
            World world=worldMap.get(key);
            world.initWorld();
        }
        AfterDlcsThread.stop=false;
    }
}
